﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;

namespace Trans
{
    class Program
    {

        static void Main(string[] args)
        {
            string source = Properties.Settings.Default.sourceCode;
//@"PROGRAM NOMER_11;
// VAR X,Y,XX: REAL;
// I:INTEGER;
// BEGIN
//  1: READ(X);
//  XX := 1+X;
//  FOR I := 1 TO 10.0 DO
//   Y:=1*XX-2.12/I;
//  IF Y-X*X <= 20.0 THEN 
//   WRITE(Y,X) 
//  ELSE GOTO 1
// END.";
            List<string> terminalWords = Properties.Settings.Default.terminalWords.Trim().Split(',').ToList<string>();
                // new List<string>() { "PROGRAM", "VAR", "REAL", "INTEGER", "BEGIN", "FOR", "TO", "DO", "IF", "THEN", "ELSE", "END" };


            LexicalAnalyzer LA = new LexicalAnalyzer();
            List<Lexem> lexems;
            Dictionary<string, int> ids;
            Dictionary<string, int> consts;
            List<string> errors;
            bool parsed = LA.Scan(source, terminalWords, out lexems, out ids, out consts, out errors);

            if (parsed)
            {
                foreach (Lexem l in lexems)
                {
                    Console.Write(l.ToString());
                }
            }

            Console.WriteLine();
            Console.WriteLine("Errors: ");
            int errorCount = 0;
            foreach (var error in errors)
            {
                Console.WriteLine("Error {0}: {1}", errorCount++, error);
            }

            Console.WriteLine();
            Console.WriteLine("Tables:");
            Console.WriteLine(" #   id");
            foreach (KeyValuePair<string, int> kp in ids)
            {
                Console.WriteLine("{0,2} {1}", kp.Value, kp.Key);
            }

            Console.WriteLine();
            Console.WriteLine(" #   const");
            foreach (KeyValuePair<string, int> kp in consts)
            {
                Console.WriteLine("{0,2} {1}", kp.Value, kp.Key);
            }

            Console.ReadKey();
        }
    }
}
